package com.example.adminsystem.controller;

import com.example.adminsystem.entity.User;
import com.example.adminsystem.repository.UserRepository;
import com.example.adminsystem.req.UserReq;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
@Schema(description = "用户管理接口")
public class UserController extends BaseCrudController<User, Long, UserReq.AddUserRequest, UserReq.UpdateUserRequest, UserReq.UserQuery> {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    protected UserRepository getRepository() {
        return userRepository;
    }



    @Override
    protected User convertToEntityForCreate(UserReq.AddUserRequest dto) {
        // 重写新增方法，对密码进行加密
        User user = new User();
        user.setUsername(dto.getUsername());
        user.setNickname(dto.getNickname());
        user.setPassword(passwordEncoder.encode(dto.getPassword()));
        user.setEmail(dto.getEmail());
        return user;
    }
//
//    @Override
//    protected User convertToEntityForUpdate(UpdateUserRequest dto) {
//        // 重写编辑方法
//        User user = new User();
//        user.setNickname("我是测试昵称");
//        return user;
//    }
}
